package com.quwan.manager.controller;

import com.quwan.app.service.MemberService;
import com.quwan.common.exception.ExpCode;
import com.quwan.common.result.ApiResult;
import com.quwan.common.result.PageResult;
import com.quwan.entity.TMember;
import com.quwan.manager.controller.dto.BalanceChangeLog;
import com.quwan.manager.controller.dto.MemberDTO;
import com.quwan.manager.controller.dto.MemberDetailDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

/**
 * @author quan
 * @date 2021-06-07 11:44
 */
@Api(tags = "后台管理接口")
@RestController("managerMember")
@RequiredArgsConstructor
@RequestMapping("mgr")
public class MemberController {
    private final PasswordEncoder passwordEncoder;
    private final MemberService memberService;

    @ApiOperation("会员列表")
    @GetMapping("/users")
    public ApiResult<PageResult<MemberDTO>> members(@ApiParam(value = "页码，1开始，默认1") @RequestParam(required = false, defaultValue = "1") int page,
                                                    @ApiParam(value = "页大小，默认10") @RequestParam(required = false, defaultValue = "2") int size,
                                                    @ApiParam(value = "用户名") @RequestParam(required = false) String nikeName,
                                                    @ApiParam(value = "手机号") @RequestParam(required = false) String mobile,
                                                    @ApiParam(value = "id") @RequestParam(required = false) Long id,
                                                    @ApiParam(value = "autoId") @RequestParam(required = false) Integer autoId,
                                                    @ApiParam(value = "余额范围最小值") @RequestParam(required = false) Long min,
                                                    @ApiParam(value = "余额范围最大值") @RequestParam(required = false) Long max,
                                                    @ApiParam(value = "注册时间范围最小值，格式：2021-01-01") @RequestParam(required = false) LocalDate minDate,
                                                    @ApiParam(value = "注册时间范围最大值，格式：2021-02-01") @RequestParam(required = false) LocalDate maxDate,
                                                    @ApiParam(value = "启用：0，禁用：1") @RequestParam(required = false) Integer status,
                                                    @ApiParam(value = "真实姓名") @RequestParam(required = false) String name,
                                                    @ApiParam(value = "城市") @RequestParam(required = false) String area,
                                                    @ApiParam(value = "系统") @RequestParam(required = false) String source
                                                    ) {
        return ApiResult.success(memberService.users(page, size, nikeName, mobile, id, min, max, minDate, maxDate, status, name, area,source,autoId));
    }

    @ApiOperation("新增会员 不要传时间字段")
    @PostMapping("user/add")
    public ApiResult addMember(TMember member) {
        if (StringUtils.isNotBlank(member.getName())) {
            boolean b = memberService.getMemberByColumn("name", member.getName());
            if (b) {
                return ApiResult.error("会员真实姓名已存在");
            }
        }
        if (StringUtils.isNotBlank(member.getMobile())) {
            boolean b = memberService.getMemberByColumn("mobile", member.getMobile());
            if (b) {
                return ApiResult.error("该手机号已存在");
            }
        }
        if (StringUtils.isNotBlank(member.getNickName())) {
            boolean b = memberService.getMemberByColumn("nickName", member.getNickName());
            if (b) {
                return ApiResult.error("该用户昵称已存在");
            }
        }

        if (memberService.addMember(member))
            return ApiResult.success();

        return ApiResult.failed();

    }

    @ApiOperation("开启/禁用 会员")
    @PostMapping("user/changeStatus")
    public ApiResult changeStatus(@ApiParam("会员id") @RequestParam String mId,
                                  @ApiParam("0正常 1禁用") @RequestParam String status) {
        if (memberService.changeStatus(mId.trim(), status))
            return ApiResult.success();

        return ApiResult.failed();

    }

    @ApiOperation("用户余额变更记录")
    @GetMapping("user/balanceLog")
    public ApiResult<PageResult<BalanceChangeLog>> memberBalanceLog(@ApiParam(value = "页码，1开始，默认1") @RequestParam(required = false, defaultValue = "1") int pageNo,
                                                                    @ApiParam(value = "页大小，默认10") @RequestParam(required = false, defaultValue = "10") int pageSize,
                                                                    @ApiParam("会员id") @RequestParam String mId,
                                                                    @ApiParam(value = "开始时间范围最小值，格式：2021-01-01") @RequestParam(required = false) LocalDate beginTme,
                                                                    @ApiParam(value = "结束时间范围最大值，格式：2021-02-01") @RequestParam(required = false) LocalDate endTime,
                                                                    @ApiParam(value = "查询类型 不传条件 默认查询所有  0消费 1充值 2提现 3订单取消退回 4完成订单") @RequestParam(required = false) String type) {
        return ApiResult.success(memberService.memberBalanceLog(pageNo, pageSize, mId, beginTme, endTime, type));
    }

    @ApiOperation("会员详细信息")
    @GetMapping("user/details")
    public ApiResult<MemberDetailDTO> memberInfo(@ApiParam("会员id") @RequestParam String mId) {
        return ApiResult.success(memberService.getMemberDetailsById(mId));
    }

    @ApiOperation("重置密码/修改密码")
    @PostMapping("user/resetPassword")
    public ApiResult resetPassword(@ApiParam("会员id") @RequestParam(required = true) String autoId,
                                   @ApiParam(value = "新密码") @RequestParam(required = true) String newPwd,
                                   @ApiParam(value = "确认密码") @RequestParam(required = true) String conPwd) {
        if (!newPwd.equals(conPwd)) {
            return ApiResult.error(ExpCode.PASSWORD_NOT_SAME.getMsg());
        }
        TMember member = new TMember();
        member.setAutoId(Long.valueOf(autoId));
        member.setPassword(passwordEncoder.encode(newPwd));
        if (memberService.resetMemberPassword(member))
            return ApiResult.success();
        return ApiResult.failed();
    }

    @ApiOperation("真人审核管理列表")
    @GetMapping("user/usersCheckedList")
    public ApiResult<PageResult<MemberDTO>> usersCheckedList(@ApiParam(value = "页码，1开始，默认1") @RequestParam(required = false, defaultValue = "1") int page,
                                                             @ApiParam(value = "页大小，默认10") @RequestParam(required = false, defaultValue = "10") int size,
                                                             @ApiParam(value = "用户名") @RequestParam(required = false) String name,
                                                             @ApiParam(value = "用户主键Id") @RequestParam(required = false) String autoId,
                                                             @ApiParam(value = "审核状态 -1审核不通过 0未认证 1审核中 2已认证") @RequestParam(required = false) Integer faceStatus) {
        return ApiResult.success(memberService.usersCheckedList(page, size, name, faceStatus,autoId));
    }

    @ApiOperation("真人审核——审核状态修改")
    @PostMapping("user/checkedFaceStatus")
    public ApiResult checkedFaceStatus(@ApiParam("会员id") @RequestParam(required = true) String autoId,
                                       @ApiParam(value = "审核状态 -1审核不通过 0未认证 1审核中 2已认证") @RequestParam(required = true) Integer faceStatus) {
        if (memberService.checkedFaceStatus(autoId, faceStatus))
            return ApiResult.success();
        return ApiResult.failed();
    }

    @ApiOperation("实名认证列表")
    @PostMapping("user/identityList")
    public ApiResult<PageResult<MemberDTO>> usersIdentityList(@ApiParam(value = "页码，1开始，默认1") @RequestParam(required = false, defaultValue = "1") int page,
                                                              @ApiParam(value = "页大小，默认10") @RequestParam(required = false, defaultValue = "10") int size,
                                                              @ApiParam(value = "主键Id") String id,
                                                              @ApiParam(value = "用户Id") String autoId,
                                                              @ApiParam(value = "审核状态 -1审核不通过 0未认证 1审核中 2已认证") @RequestParam(required = false, defaultValue = "1") Integer status) {
        return ApiResult.success(memberService.usersIdentityList(page, size, status, autoId,id));
    }

    @ApiOperation("实名认证 状态修改")
    @PostMapping("user/identityStatus")
    public ApiResult identityStatus(@ApiParam("会员id") @RequestParam(required = true) String autoId,
                                       @ApiParam(value = "审核状态 -1审核不通过 0未认证 1审核中 2已认证") @RequestParam(required = true) Integer status) {
        if (memberService.identityStatus(autoId, status))
            return ApiResult.success();
        return ApiResult.failed();
    }
}
